package com.bootdo.common.controller;

import org.springframework.stereotype.Controller;
import com.bootdo.common.utils.ShiroUtils;
import com.bootdo.system.domain.UserDO;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.regex.Pattern;

@Controller
public class BaseController {
	public UserDO getUser() {
		return ShiroUtils.getUser();
	}

	public Long getUserId() {
		return getUser().getUserId();
	}

	public String getUsername() {
		return getUser().getUsername();
	}

	/**得到request对象
	 * @return
	 */
	public HttpServletRequest getRequest() {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
		return request;
	}

	/**
	 * 判断当前登录的用户是否为数字。
	 * @return 如果为数字返回数字-10000，否则返回null
	 */
	public Integer isInteger(String str) {

		Integer integ=null;
		/*进行判断是否为数字*/
		Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
		boolean b = pattern.matcher(str).matches();;
		/*判断用户登录名是否为数字*/
		if(b){
			integ = Integer.parseInt(str);
			return integ;
		}
		return null;
	}
}